/**********************************************************************/
/*
Author: Karan Makkar
Created: Nov 2023
Description: Attrition Checks
Output: Table A3
*/
/**********************************************************************/


/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

clear all
set more off
set matsize 11000

* Log
cap log close
global prefix: display %tdCYND td(`c(current_date)')
log using "$KP_logs/${prefix}_onlinesurvey_responserates.txt", text replace

/*----------------------------------------------------*/
* Section: Run programs and set dataset filepaths
/*----------------------------------------------------*/

* Regression programs
include "$KP_do/3 Functions/impact_regression_programs"

* Table formatting and flipping programs
include "$KP_do/3 Functions/impact_table_programs"

/*----------------------------------------------------*/
* Section: Regression Specifications
/*----------------------------------------------------*/
* SE CLUSTERING (OLS): Individual level
global vce vce(cluster anon_id4)

* INSTRUMENT (IV): win in batch
global instrument_iv win_in_batch

* SE CLUSTERING (IV): Individual level
global cluster cluster(anon_id4)

* FIXED EFFECTS: Inputs into PMO-assigned weight interacted by batch
global demog_batch absorb(gender#batch urban#batch ed_cat#batch)

/*----------------------------------------------------------------*/
* Section: Data Setup
/*----------------------------------------------------------------*/

* Load 2021 data
use "$KP_deid_survey/2021/Clean/071323_survey_21_deid_clean_merged.dta", clear

 *Keep only those sent survey.
keep if sent_survey==1 | !mi(progress)

gen round = 2021

* Flag respondents
gen respondent21 = completed == 1 & consented == 1 & verified == 1 

* Append 2022 data
append using "$KP_deid_survey/2022/Clean/072125_survey_21_22_deid_clean_control_merged.dta"
 *Keep only those sent survey. Commenting out for now since sent survey isn't correct for '22 survey
// keep if sent_survey==1 | !mi(progress)

replace round = 2022 if mi(round)

* Flag respondents
gen respondent22 = completed == 1 & consented == 1 & verified == 1 if round == 2022

* X VAR: Generate placeholder
gen x = 0
la var x "Treated"
gen treat =.

*Education cat
recode education (1/2 = 1) (3 = 2) (4/8 = 3), gen(ed_cat) 

cap drop age_cat
recode age (0/30 = 1) (31/50 = 2) (51/100 = 3), gen(age_cat) 

* Cleaning (move to cleaning file later)
replace yrs_school = 13 if yrs_school==13.5
replace temp_no_work = . if missing(employed)

* Drop non-randomized batches
drop if inlist(batch, 1, 15)
/*----------------------------------------------------------------*/
* Section: Regressions
/*----------------------------------------------------------------*/

* loop through each variable of table
foreach y of varlist respondent22 respondent21 {

    * Set precomma and treatment var
    if "`y'" == "respondent21"{
        local precomma = "round ==2021 & batch >= 7"
        replace treat = ever_win_17
    } 
    if "`y'" == "respondent22"{
        local precomma = "round ==2022 & batch >= 18"
        replace treat = ever_win_47
    } 
    
    * OLS
    replace x = win_in_batch

    * Strata P(win only)
    ols_reg `y'_c1_ols `y' x "stratum_win_prob c.stratum_win_prob#i.batch" "`precomma'" "noabsorb" "$vce"
    * Also demog controls
    ols_reg `y'_c2_ols `y' x "stratum_win_prob c.stratum_win_prob#i.batch" "`precomma'" "$demog_batch" "$vce"

    * IV
    replace x = treat

    * Strata P(win only)
    iv_reg `y'_c1_iv `y' x $instrument_iv "stratum_win_prob c.stratum_win_prob#i.batch" "`precomma'" "" "$cluster"
    * Also demog controls
    iv_reg `y'_c2_iv `y' x $instrument_iv "stratum_win_prob c.stratum_win_prob#i.batch" "`precomma'" "$demog_batch" "$cluster"
}

* Labeling
label def treat 0 "Control" 1 "Treated"
label val x treat

esttab respondent22_c1_ols respondent22_c1_iv respondent22_c2_ols respondent22_c2_iv using "$KP_output/tables/descriptive/tex/TableA3.tex", ///
b(%9.3f) se(%9.3f) ///
keep(1.x) ///
collabels(none) ///
label ///
nonum ///
star(* 0.10 ** 0.05 *** 0.01) /// 
mtitles("OLS" "IV" "OLS" "IV") ///
stats(N control_mean, label("Observations" "Control Mean") fmt(%9.0fc %9.3fc)) ///
prehead(`"\hline \hline"') ///
nolines posthead(`"& (1) & (2) & (3)& (4)  \\"' `"\hline \\ \multicolumn{4}{l}{\textit{Panel A: 2022 Online Survey}} \\\\[-2.5ex]"') ///
postfoot(`"Strata P(Win) Controls & Y & Y & Y & Y \\"' `"Demographic Controls & N & N & Y & Y \\"' `"Batches & 18-22 & 18-22 & 18-22 & 18-22 \\"' `"\hline"') ///
fragment ///
title(Online Survey Regressions: Survey Response Rate) ///
replace

esttab respondent21_c1_ols respondent21_c1_iv respondent21_c2_ols respondent21_c2_iv using "$KP_output/tables/descriptive/tex/TableA3.tex", ///
b(%9.3f) se(%9.3f) ///
keep(1.x) ///
collabels(none) ///
label ///
star(* 0.10 ** 0.05 *** 0.01) /// 
nomtitles nonumbers ///
stats(N control_mean, label("Observations" "Control Mean") fmt(%9.0fc %9.3fc)) ///
nolines posthead(`"\\ \multicolumn{4}{l}{\textit{Panel B: 2021 Online Survey}} \\\\[-2.5ex]"') ///
postfoot(`"Strata P(Win) Controls & Y & Y & Y & Y \\"' `"Demographic Controls & N & N & Y & Y \\"' `"Batches & 7-14, 16-17 & 7-14, 16-17 & 7-14, 16-17 & 7-14, 16-17 \\"' `"\hline"') ///
fragment ///
append